﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DataBase;

public partial class UserLogin : System.Web.UI.Page
{
    public DBPlayerInfo playerInfo = new DBPlayerInfo();


    protected void Page_Load(object sender, EventArgs e)
    {
        checkRadioButtons();
    }

    private void checkRadioButtons()
    {
        if (this.RadioButton1.Checked == true)
        {

            this.userTextBox.Enabled = true;
            this.passwordTextBox.Enabled = true;

            this.createEmailTextBox.Enabled = false;
            this.createPasswordTextBox.Enabled = false;
            this.createUserTextBox.Enabled = false;
            this.confirmCreatePasswordTextBox.Enabled = false;
            this.CreateUserButton.Enabled = false;
            this.confirmButton.Enabled = true;
            this.Page.Form.DefaultButton = this.confirmButton.UniqueID;
        }
        else if (this.RadioButton2.Checked == true)
        {

            this.userTextBox.Enabled = false;
            this.passwordTextBox.Enabled = false;

            this.createEmailTextBox.Enabled = true;
            this.createPasswordTextBox.Enabled = true;
            this.createUserTextBox.Enabled = true;
            this.confirmCreatePasswordTextBox.Enabled = true;
            this.CreateUserButton.Enabled = true;
            this.confirmButton.Enabled = false;
            this.Page.Form.DefaultButton = this.CreateUserButton.UniqueID;

        }
    }


    protected void confirmButton_Click(object sender, EventArgs e)
    {
        DBAdapter dba = new DBAdapter();
        DBPlayerInfo pi = dba.checkUserPassword(this.userTextBox.Text, this.passwordTextBox.Text);

        if (pi!=null)
        {
            this.playerInfo.ID = pi.ID;
            this.playerInfo.Username = pi.Username;
            this.playerInfo.Email = pi.Email;

            this.confirmButton.Text = "Login Secceeded";
        }
        else this.confirmButton.Text = "Login Failed";
        
                
    }

    private bool checkInputValidity(string name, string password, string email)
    {
        DBAdapter dba = new DBAdapter();
        return (DBLibrary.checkEmailValidity(email) && (dba.checkUsername(name)==null));
    }
    protected void CreateUser_ButtonClick(object sender, EventArgs e)
    {
        DBAdapter dba = new DBAdapter();
        bool paramsOk = false;
        bool passwordsMatch = (this.confirmCreatePasswordTextBox.Text == this.createPasswordTextBox.Text);
        if (passwordsMatch) paramsOk = checkInputValidity(this.createUserTextBox.Text, this.createPasswordTextBox.Text, this.createEmailTextBox.Text);

        bool success = false;

        if (paramsOk) success = dba.addAccount(this.createUserTextBox.Text, this.createPasswordTextBox.Text, this.createEmailTextBox.Text);

        if (success)
        {
            Response.Write("<B> User " + this.createUserTextBox.Text + " created successfully");
            this.createUserTextBox.Text = "";
            this.createPasswordTextBox.Text = "";
            this.confirmCreatePasswordTextBox.Text = "";
            this.createEmailTextBox.Text = "";
        }
        else
        {
            if (!dba.isUserNameAvailable(this.createUserTextBox.Text)) Response.Write("<B> User name " + this.createUserTextBox.Text + " is already taken <br> Please select a different user name <br>");
            else if (!passwordsMatch) Response.Write("<B>Passwords do not match<br>");
            else if (!DBLibrary.checkEmailValidity(this.createEmailTextBox.Text)) Response.Write("<B> please make sure to enter a valid E-Mail address<br>");
            else Response.Write("<B> All fields are requiered<br>");
            Response.Write("<br>");
        }


    }

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        checkRadioButtons();
    }
}
